package com.guoqi.marrydate.flag;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

/**
 * 通用适配器
 */
public abstract class CommonAdapter<T> extends BaseAdapter {

    protected Context context;
    protected List<T> datas;
    protected LayoutInflater inflater;
    private int layoutId;
    private int position;


    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    public CommonAdapter(Context context, List<T> datas, int layoutId) {
        super();
        this.context = context;
        this.datas = datas;
        this.inflater = LayoutInflater.from(context);
        this.layoutId = layoutId;
    }

    @Override
    public int getCount() {
        return datas.isEmpty() ? 0 : datas.size();
    }

    @Override
    public T getItem(int position) {
        return datas.isEmpty() ? null : datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 更新数据
     *
     * @param datas
     */
    public void resetData(List<T> datas) {
        this.datas = datas;
        this.notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        setPosition(position);
        ViewHolder holder = ViewHolder.get(context, convertView, parent, layoutId, position);
        convert(holder, getItem(position));
        return holder.getConvertView();
    }

    public abstract void convert(ViewHolder holder, T t);
}
